package com.example.neusoft.startproject_wk;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.neusoft.startproject_wk.Iface.WeatherListener;
import com.example.neusoft.startproject_wk.bean.WeatherInfo;
import com.example.neusoft.startproject_wk.model.Weather51Model;
import com.example.neusoft.startproject_wk.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, WeatherListener {
    private EditText etcityno;
    private Button btnsearch;
    private TextView tvcity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        etcityno = (EditText) findViewById(R.id.editText);
        tvcity = (TextView) findViewById(R.id.textView);
        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                //实例化模型1，获取天气信息
                WeatherModel weatherModel =new WeatherModel();
                weatherModel.getWeather(etcityno.getText().toString(),this);
                break;
            case R.id.button2:
                //实例化模型2，获取天气信息
                Weather51Model weather51Model=new Weather51Model();
                weather51Model.getWeather(etcityno.getText().toString(),this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResponse(WeatherInfo weatherInfo) {
        if (weatherInfo != null) {
            tvcity.setText(weatherInfo.getCity()+weatherInfo.getWD()+weatherInfo.getWS());
        } else {
            tvcity.setText("未知");
        }
    }

    @Override
    public void onFail(String msg) {
        tvcity.setText(msg);
    }
}
